﻿using System.IO;
using System.Linq;
using System.Web.Mvc;
using FortuneArterial.Site.BusinessLogicLayer.Entity;
using FortuneArterial.Site.Model.Site;
using Newtonsoft.Json;
using Bll = FortuneArterial.Site.BusinessLogicLayer;

namespace FortuneArterial.Site.Manage.Controllers
{
    public class PageController : Controller
    {
        public Bll.Database.Context context = new Bll.Database.Context();

        public ActionResult Index(string parent)
        {
            ViewBag.ParentID = parent;
            return PartialView(context.PageInfoSet.Where(m => m.ParentID == parent).OrderBy(m => m.OrderNum));
        }

        [HttpGet]
        public ActionResult Create(string parent)
        {
            return PartialView("Edit", new PageInfo() { ParentID = parent });
        }

        [HttpGet]
        public ActionResult Edit(string id)
        {
            var site = context.PageInfoSet.FirstOrDefault<PageInfo>(m => m.ID == id);
            return PartialView("Edit", site);
        }

        [HttpPost]
        public ActionResult Edit(string id, PageInfo pageInfo)
        {
            PageInfo page;
            if (string.IsNullOrEmpty(id))
            {
                page = EntityFactory.CreateEntity<PageInfo>();
                context.PageInfoSet.Add(page);
            }
            else
            {
                page = context.PageInfoSet.FirstOrDefault<PageInfo>(m => m.ID == id);
            }
            page.Icon = pageInfo.Icon;
            page.Name = pageInfo.Name;
            page.Path = pageInfo.Path;
            page.ParentID = pageInfo.ParentID;
            page.OrderNum = pageInfo.OrderNum;

            context.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult ExportData()
        {
            string dataPath = Server.MapPath("~/temp/data/");
            if (!Directory.Exists(dataPath))
            {
                Directory.CreateDirectory(dataPath);
            }

            var datas = context.PageInfoSet;
            using (TextWriter tw = new StreamWriter(Path.Combine(dataPath, "PageInfo.json"), false))
            {
                foreach (var page in datas)
                {
                    tw.WriteLine(JsonConvert.SerializeObject(page));
                }
            }

            return Json(new { Success = true }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult ImportData()
        {
            string dataPath = Server.MapPath("~/temp/data/");
            using (TextReader tr = new StreamReader(Path.Combine(dataPath, "PageInfo.json")))
            {
                while (true)
                {
                    var str = tr.ReadLine();
                    if (string.IsNullOrEmpty(str))
                    {
                        break;
                    }

                    var page = JsonConvert.DeserializeObject<PageInfo>(str);
                    context.PageInfoSet.Add(page);
                }
            }
            context.SaveChanges();

            return Json(new { Success = true }, JsonRequestBehavior.AllowGet);
        }

    }
}
